﻿using UnityEngine;
using System.Collections;

public class SupplyWpnBox : MonoBehaviour {
    public int m_iThisPrice;

    public int m_iNumOfBuyingObj;
    int m_iBeforeBuyingNum;

    public WpnCategorize m_SupplyWpn;
    public bool m_bExample;
	// Use this for initialization
	void Start () {
        m_iThisPrice = 0;
        m_iNumOfBuyingObj = 0;
	}
	
	// Update is called once per frame
	void Update () {
        if (!m_bExample)
            transform.FindChild("CountBox").FindChild("Label").GetComponent<UILabel>().text = string.Format("{0}", m_iNumOfBuyingObj);

	}

    void LateUpdate()
    {
        if(m_iBeforeBuyingNum != m_iNumOfBuyingObj)
        {
            m_iThisPrice = m_iNumOfBuyingObj * m_SupplyWpn.m_WpnData.m_iPrice;
            transform.FindChild("Price").GetComponent<UILabel>().text = string.Format("$ {0}", m_iThisPrice);
            transform.parent.GetComponent<SupplyBills>().CalculateTotalPrice();
        }

        m_iBeforeBuyingNum = m_iNumOfBuyingObj;
    }

    public void Init()
    {
        if (m_bExample)
        {
            transform.GetComponent<UISprite>().spriteName = "SupplyWpnBox1";
            transform.FindChild("WpnName").GetComponent<UILabel>().text = "Weapon Name";
            transform.FindChild("WpnCount").GetComponent<UILabel>().text = "NUM.";
            transform.FindChild("WpnCount").GetComponent<UILabel>().fontSize = 55;
            transform.FindChild("Price").GetComponent<UILabel>().text = "Price";
            if (transform.FindChild("SupplyBar") != null)
            {
                Destroy(transform.FindChild("SupplyBar").gameObject);
                Destroy(transform.FindChild("SupplyBarPointer").gameObject);
                Destroy(transform.FindChild("MaxBox").gameObject);
                Destroy(transform.FindChild("CountBox").gameObject);
            }
        }
        else
        {
            transform.FindChild("WpnName").GetComponent<UILabel>().text = m_SupplyWpn.m_WpnData.m_strWeaponName;
            transform.FindChild("WpnCount").GetComponent<UILabel>().text = string.Format("{0}", m_SupplyWpn.m_iNumberOfWpn);

            transform.FindChild("CountBox").FindChild("Label").GetComponent<UILabel>().depth += GetComponent<UISprite>().depth;
            transform.FindChild("SupplyBarPointer").GetComponent<SupplyBarPointer>().m_iObjNum = m_SupplyWpn.m_iNumberOfWpn;
        }

        for (int i = 0; i < transform.childCount; ++i)
        {
            if (transform.GetChild(i).GetComponent<UILabel>())
                transform.GetChild(i).GetComponent<UILabel>().depth = GetComponent<UISprite>().depth + 2;
            else if (transform.GetChild(i).GetComponent<UISprite>())
                transform.GetChild(i).GetComponent<UISprite>().depth = GetComponent<UISprite>().depth + 2;
        }
    }
}
